﻿using System.Reflection;

namespace Gengens.Disk.Entity.bean
{
    public class ModelInfo
    {
        private Type _type;

        public Type Type
        {
            get => _type;
            set {
                string baseTypeFullName = typeof(BaseModel<>).GetGenericTypeDefinition().FullName;
                Type theBaseType = value.BaseType;

                if (theBaseType == default || !theBaseType.IsGenericType || theBaseType.GetGenericTypeDefinition().FullName != baseTypeFullName) {
                    throw new ArgumentException("非BaseModel的派生类，不可注册为模型信息");
                }
                
                _type = value; 
            }
        }

        public object _instance;
        public object Instance
        {
            get
            {
                _instance ??= Activator.CreateInstance(Type);
                return _instance;
            }
        }

        public ModelInfo(Type type)
        {
            Type = type;
        }


    }
}
